<template>
  <div class="login-page">
    <el-card class="box-card">
      <el-form class="demo-ruleForm"   ref="form" :model="form" :rules="rules" autocomplete="off">
        <el-image
          class="imglogo"
          style="width: 100px; height: 100px"
          src="http://likede2-admin.itheima.net/img/logo.595745bd.png"
        ></el-image>
        <el-form-item prop="loginName">
          <el-input
            placeholder="请输入账户"
            v-model="form.loginName"
            prefix-icon="el-icon-mobile-phone"
            maxlength="18"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            placeholder="请输入密码"
            v-model="form.password"
            :show-password="true"
            prefix-icon="el-icon-lock"
          ></el-input>
        </el-form-item>
        <el-form-item prop="securitycode">
          <el-input
          class="security"
            placeholder="请输入验证码"
            v-model="form.securitycode"
            prefix-icon="el-icon-circle-check"
            maxlength="4"
          ></el-input>
          <div class="securitycode" @click="clickFn"><img :src="imgSrc" alt=""></div>
        </el-form-item>
        <el-form-item>
          <el-button  @click="submitForm()">登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { SecurityCode, Login } from '@/api/user'
export default {
  data () {
    return {
      rules: {
        loginName: [
          { required: true, message: '请输入用户名', trigger: ['blur', 'change'] },
          { min: 1, max: 18, message: '长度在 1 到 18 个字符', trigger: ['blur', 'change'] }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: ['blur', 'change'] },
          { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: ['blur', 'change'] }
        ],
        securitycode: [
          { required: true, message: '请输入验证码', trigger: ['blur', 'change'] }
        ]
      },
      form: {
        loginName: 'admin',
        password: 'admin',
        securitycode: ''
      },
      imgSrc: '',
      clientToken: ''
    }
  },
  methods: {
    async submitForm () {
      try {
        const valid = await this.$refs.form.validate()
        if (valid) {
          const res = await Login({
            loginName: this.form.loginName,
            password: this.form.password,
            code: this.form.securitycode,
            clientToken: this.clientToken
          })
          if (res.success) {
            this.$router.push('/')
            this.$store.commit('user/setUserToken', { token: res.token, userName: res.userName })
            this.$message.success(res.msg)
          } else {
            this.$message.error(res.msg)
          }
        }
      } catch (e) {
        console.log(e)
      }
    },
    /* 封装axios 请求验证码 */
    fn () {
      this.clientToken = Math.random() * 100 + ''
      SecurityCode(this.clientToken).then(response => {
        return 'data:image/png;base64,' + btoa(
          new Uint8Array(response)
            .reduce((data, byte) => data + String.fromCharCode(byte), ''))
      }).then(data => {
        this.imgSrc = data
        // console.log(this.imgSrc)
      })
    },
    /* 点击更新验证码的 */
    clickFn () {
      this.fn()
    }
  },
  created () {
    this.fn()
  }
}
</script>

<style lang="scss" scoped>
.login-page {
  min-height: 100vh;
  background: url("@/assets/bgc.png") no-repeat center / cover;
  display: flex;
  align-items: center;
  justify-content: space-around;
}
.box-card {
  width: 480px;
  margin: 0 auto;
  padding: 50px 10px 0;
  position: relative;
  overflow: inherit;
  border-radius: 10px;
}
.imglogo {
  position: absolute;
  top: -44px;
  left: 50%;
  margin-left: -48px;
}
::v-deep .el-input {
  height: 48px;
  background: #fff;
  border: 1px solid #e2e2e2;
  border-radius: 5px;
  line-height: 48px;
  .el-input__inner {
    border: 0px;
    color: #999;
  }
}
::v-deep .el-button {
  width: 100%;
  height: 52px;
  background: linear-gradient(262deg, #2e50e1, #6878f0);
  opacity: 0.91;
  border-radius: 8px;
  color: #fff;
  text-shadow: 0 7px 22px #cfcfcf;
  span {
    color: #fff;
  }
}
.securitycode {
  position: absolute;
  right:  0;
  bottom: 0;
  width: 124px;
  height: 48px;
  overflow: hidden;
  img {
    width: 100%;
  }
}
</style>
